package com.atguigu.spzx.manager.service.impl;

import cn.hutool.core.util.IdUtil;
import com.atguigu.spzx.manager.mapper.ProductDetailsMapper;
import com.atguigu.spzx.manager.mapper.ProductMapper;
import com.atguigu.spzx.manager.mapper.ProductSkuMapper;
import com.atguigu.spzx.manager.service.ProductService;
import com.atguigu.spzx.model.dto.product.ProductDto;
import com.atguigu.spzx.model.dto.product.ProductSearchDto;
import com.atguigu.spzx.model.entity.product.Product;
import com.atguigu.spzx.model.entity.product.ProductDetails;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.vo.product.ProductVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    ProductMapper productMapper;
    @Resource
    ProductSkuMapper productSkuMapper;
    @Resource
    ProductDetailsMapper productDetailsMapper;
    @Override
    public PageInfo<ProductVo> page(Integer pageNum, Integer pageSize, ProductSearchDto searchDto) {
        PageHelper.startPage(pageNum,pageSize,true , true , null);

        return new PageInfo<>(productMapper.selectPage(searchDto));
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(ProductDto productDto) {
        //1、保存spu数据
        Product product = new Product();
        BeanUtils.copyProperties(productDto,product);
        product.setCreateTime(new Date());
        product.setUpdateTime(new Date());
        //保存后需要返回spu的id
        productMapper.save(product);
        //2、保存spu的图片详情
        ProductDetails productDetails = new ProductDetails();
        productDetails.setId(product.getId());
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(productDto.getDetailsImageUrls());
        productDetails.setUpdateTime(new Date());
        productDetails.setCreateTime(new Date());
        productDetailsMapper.save(productDetails);
        //3、保存sku数据列表
        List<ProductSku> productSkuList = productDto.getProductSkuList();
        productSkuList.forEach(productSku -> {
            //sku绑定自己的spu
            productSku.setProductId(product.getId());
            //设置商品编号： 展示给用户 或者和第三方交互时代表唯一一个商品的字段值
            //UUID生成32位长度的16进制字符组成的字符串 ，唯一、无序、有字母

            //使用雪花算法生成全局唯一的 按照时间戳递增的有序的 19位的10进制的id
            String code = IdUtil.createSnowflake(1L,1L).nextId()+"";
            productSku.setSkuCode(code);

            productSku.setStatus(0);
            productSku.setSkuName(product.getName()+" "+productSku.getSkuSpec());
            productSku.setCreateTime(new Date());
            productSku.setUpdateTime(new Date());
        });

        productSkuMapper.saveBatch(productSkuList);

    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(Long id) {
        //删除spu
        productMapper.deleteById(id);
        //删除spu details图片
        productDetailsMapper.deleteById(id);
        //删除sku
        productSkuMapper.deleteBatchByProductId(id);

    }

    @Override
    public ProductDto getById(Long id) {
        return productMapper.selectById(id);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateById(ProductDto productDto) {
        Product product = new Product();
        BeanUtils.copyProperties(productDto,product);
        product.setUpdateTime(new Date());
        productMapper.updateById(product);

        ProductDetails productDetails = new ProductDetails();
        productDetails.setId(productDto.getId());
        productDetails.setImageUrls(productDto.getDetailsImageUrls());
        productDetails.setUpdateTime(new Date());
        productDetailsMapper.updateById(productDetails);

        List<ProductSku> productSkuList = productDto.getProductSkuList();
//        productSkuList.
        productSkuList.forEach(productSku ->{
            productSku.setSkuName(product.getName()+" "+productSku.getSkuSpec());
            productSku.setUpdateTime(new Date());
            productSkuMapper.updateById(productSku) ;
        });

    }

    @Override
    public void updateAuditStatusById(Long id, Integer auditStatus) {
        productMapper.updateAuditStatusById(id , auditStatus);
    }

    @Override
    public void updateStatusById(Long id, Integer status) {
        productMapper.updateStatusById(id , status);
    }
}
